package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.ShareService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;

/**
 * 桩友圈控制器
 * @author xlf
 *
 */
@RestController
@RequestMapping("/share")
public class ShareController {

	/**桩友圈业务接口*/
	@Resource
	private ShareService shareService;
	
	/**
	 * 桩友圈列表
	 * @param userId 用户ID
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = "/shares", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
	public String shares(
			@RequestParam(required=false,defaultValue="") String userId,
			@RequestParam(defaultValue="1") int pageNo){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = shareService.shares(userId, pageNo);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 桩友圈详细
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/sharesDetail", produces = {"application/json;charset=utf-8" }, method = RequestMethod.GET)
	public String sharesDetail(
			@RequestParam(required=false) String shareId){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = shareService.sharesDetail(shareId);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
	/**
	 * 发布新的分享
	 * @param userId
	 * @param content
	 * @param shareAddress
	 * @param areaCode
	 * @param smallImage
	 * @param middleImage
	 * @param bigImage
	 * @return
	 */
	@RequestMapping(value="/releaseShares",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String releaseShares(String userId, String content, String shareAddress, String areaCode, 
			String smallImages, String middleImages, String bigImages){

		ResultMappingEntity resultMappingEntity= null;
		try {
			String smallImage[] = null;
			String middleImage[] = null;
			String bigImage[] = null;
			if(smallImages!=null && middleImages!=null && bigImages!=null){
				smallImage = smallImages.split(",");
				middleImage = middleImages.split(",");
				bigImage = bigImages.split(",");
			}
			resultMappingEntity = shareService.releaseShares(userId, content, shareAddress, areaCode, smallImage, middleImage, bigImage);
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 桩友圈点赞
	 * @param userId
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value="/shareThumbUp",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String shareThumbUp(String userId, String shareId){

		ResultMappingEntity resultMappingEntity= null;
		try {
			resultMappingEntity = shareService.shareThumbUp(userId, shareId);
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 删除分享,通过shareId
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/deleteSharesByShareId", produces = {"application/json;charset=utf-8"}, method = RequestMethod.DELETE)
	public String deleteSharesByShareId(
		@RequestParam(value="shareId",required=true) String shareId){
	
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = shareService.deleteShares(shareId);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}

	/**
	 * 分享评论
	 * @param userId 评论用户id
	 * @param shareId 分享ID
	 * @param replyUserId 回复用户id 
	 * @param content 评论内容
	 * @param commentAddr 评论地点
	 * @return
	 */
	@RequestMapping(value = "/sharesComment", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
	public String sharesComment(
			@RequestParam(required=true) String userId, 
			@RequestParam(required=true) String shareId, 
			@RequestParam(required=false) String replyUserId, 
			@RequestParam(required=true) String content, 
			@RequestParam(value="commentAddress",required=false) String commentAddr){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = shareService.shareComment(userId, shareId, replyUserId, content, commentAddr);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
}
